package com.wzt.lianfirecontrol.fragment.baojingfuhe;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.baojingfuhe.BaoJingCheckModel;
import com.wzt.lianfirecontrol.bean.recode.user.RoleConstant;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.utils.DialogUtils;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import java.util.HashMap;
import java.util.List;

public class FuHeDetailFragment extends BaseFragment {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private TextView tv_title;
    private TextView tv_status;

    private TextView tv_time;
    private TextView tv_location;

    private LinearLayout ll_intro;
    private TextView tv_intro;

    private LinearLayout ll_device_three_photo;
    private View include_play_photo_three;
    private RelativeLayout rl_device_photo_1;
    private ImageView iv_device_photo_1;
    private ImageView iv_photo_delete_1;
    private RelativeLayout rl_device_photo_2;
    private ImageView iv_device_photo_2;
    private ImageView iv_photo_delete_2;
    private RelativeLayout rl_device_photo_3;
    private ImageView iv_device_photo_3;
    private ImageView iv_photo_delete_3;

    private Button bt_submit;

    private LinearLayout ll_fuhe_deal;
    private Button bt_zhenjing;
    private Button bt_wubao;

    private View include_progress_bar;

    private static final int SUBMIT_WHAT = 1;

    private BaoJingCheckModel baoJingFuHeModel;// type 1火警 4监管 98预警

    private Dialog zhenjingDialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            activity = getMyActivity();
            bundle = getArguments();
            if (null != bundle){
                baoJingFuHeModel = (BaoJingCheckModel) bundle.getSerializable("data");
            }
            if (baoJingFuHeModel == null) {
                activity.finish();
            }
            itemContentView = inflater.inflate(R.layout.f_fuhe_detail, container, false);
            initView();
            setDetailData();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        tv_title = itemContentView.findViewById(R.id.tv_title);
        tv_status = itemContentView.findViewById(R.id.tv_status);
        tv_time = itemContentView.findViewById(R.id.tv_time);
        tv_location = itemContentView.findViewById(R.id.tv_location);
        ll_intro = itemContentView.findViewById(R.id.ll_intro);
        tv_intro = itemContentView.findViewById(R.id.tv_intro);
        tv_title = itemContentView.findViewById(R.id.tv_title);
        tv_title = itemContentView.findViewById(R.id.tv_title);

        initPhotoThree();

        initBtnView();

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
    }

    private void initPhotoThree() {
        ll_device_three_photo = itemContentView.findViewById(R.id.ll_device_three_photo);
        include_play_photo_three = itemContentView.findViewById(R.id.include_play_photo_three);
        rl_device_photo_1 = itemContentView.findViewById(R.id.rl_device_photo_1);
        iv_device_photo_1 = itemContentView.findViewById(R.id.iv_device_photo_1);
        iv_photo_delete_1 = itemContentView.findViewById(R.id.iv_photo_delete_1);
        rl_device_photo_2 = itemContentView.findViewById(R.id.rl_device_photo_2);
        iv_device_photo_2 = itemContentView.findViewById(R.id.iv_device_photo_2);
        iv_photo_delete_2 = itemContentView.findViewById(R.id.iv_photo_delete_2);
        rl_device_photo_3 = itemContentView.findViewById(R.id.rl_device_photo_3);
        iv_device_photo_3 = itemContentView.findViewById(R.id.iv_device_photo_3);
        iv_photo_delete_3 = itemContentView.findViewById(R.id.iv_photo_delete_3);

        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        rl_device_photo_1.getLayoutParams().width = width;
        rl_device_photo_1.getLayoutParams().height = width;
        rl_device_photo_2.getLayoutParams().width = width;
        rl_device_photo_2.getLayoutParams().height = width;
        rl_device_photo_3.getLayoutParams().width = width;
        rl_device_photo_3.getLayoutParams().height = width;

    }

    private void initBtnView() {
        // 确认已处理
        bt_submit = itemContentView.findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserInfoModel.isSelfCompany(activity)){
                //    ToastUtils.showToast(activity, ConstData.ERROR_COMPANY_MSG);
                    return;
                }
                zhenjingDialog = DialogUtils.createSureAndCancelDialog(activity, "是否已处理", "取消", "确认",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // 取消
                                DialogUtils.closeDialog(zhenjingDialog);
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtils.closeDialog(zhenjingDialog);
                                initSubmitHttpHelper();
                            }
                        });
                DialogUtils.showDialog(zhenjingDialog);
            }
        });


        ll_fuhe_deal = itemContentView.findViewById(R.id.ll_fuhe_deal);
        bt_zhenjing = itemContentView.findViewById(R.id.bt_zhenjing);
        bt_wubao = itemContentView.findViewById(R.id.bt_wubao);

        // 维保人员和维保管理人员只显示预约维保单
        // 组织管理员 ROLE_ORG_MANAGER  先判断是否是组织管理员，是，就只能看，不能操作。不是的话，判断是否是维保人员
        List<String> alias = UserInfoModel.getUserInfo(getActivity()).getRoleAlias();
        if (alias.contains(RoleConstant.ROLE_ORG_MANAGER)) {
            bt_zhenjing.setVisibility(View.GONE);
            bt_wubao.setVisibility(View.GONE);
        }else {
            bt_zhenjing.setVisibility(View.VISIBLE);
            bt_wubao.setVisibility(View.VISIBLE);
        }
        bt_zhenjing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserInfoModel.isSelfCompany(activity)){
                //    ToastUtils.showToast(activity, ConstData.ERROR_COMPANY_MSG);
                    return;
                }
                if (null != baoJingFuHeModel) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("model", baoJingFuHeModel);
                    bundle.putBoolean("isZhenjing", true);
                    activity.setClickAction(ConstData.SHOWFRAG, ConstData.WUBAO, "火警描述", null, bundle);
                }
            }
        });

        bt_wubao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserInfoModel.isSelfCompany(activity)){
                //    ToastUtils.showToast(activity, ConstData.ERROR_COMPANY_MSG);
                    return;
                }
                if (null != baoJingFuHeModel) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("model", baoJingFuHeModel);
                    activity.setClickAction(ConstData.SHOWFRAG, ConstData.WUBAO, "误报描述", null, bundle);
                }
            }
        });

    }

    /**
     * 监管
     */
    private void initSubmitHttpHelper() {
        try {
            if (!Utils.hasNetwork(activity)){
                ToastUtils.showToast(activity, "网络未连接");
                return;
            }
            include_progress_bar.setVisibility(View.VISIBLE);
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", baoJingFuHeModel.getId());
            hashMap.put("userId", UserInfoModel.getUserInfo(activity).getId());
            HashMap<String, String> url_params = new HashMap<>();
            url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
            DetailHttpHelper detailHttpHelper = new DetailHttpHelper(activity, Url.BJ_CHECK_OVER_URL, SUBMIT_WHAT, handler, true, url_params);
            detailHttpHelper.setParseJsonData(new LoginParseJsonData());
            detailHttpHelper.getHttpRequest(true);
        }catch (Exception e){}
    }

    class DetailHttpHelper extends HttpHelper {

        public DetailHttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
            super(activity, url, what, handler, isPost, params);
        }

        public DetailHttpHelper(BaseActivity activity, String url, int what, Handler handler) {
            super(activity, url, what, handler);
        }
    }

    protected class LoginParseJsonData extends ParseJsonData {

        public LoginParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            switch (what){

            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
            if (ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))) {
                if (msg.what == SUBMIT_WHAT) {
                    PreferencesUtils.putString(activity, ConstData.NEEDFUHEREFRESH, "0");
                        include_progress_bar.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                include_progress_bar.setVisibility(View.GONE);
                                activity.finish();
                            }
                        }, 1000);
                }else {
                    include_progress_bar.setVisibility(View.GONE);
                }
            }else {
                include_progress_bar.setVisibility(View.GONE);
            }
            return;
        }
        switch (msg.what){
        }
    }

    /**
     * 处理详情页信息
     * @param
     */
    private void setDetailData() {
        if (!StringUtils.isEmpty(baoJingFuHeModel.getEquName())) {
            tv_title.setText(baoJingFuHeModel.getEquName());
            tv_title.setVisibility(View.VISIBLE);
        }else {
            tv_title.setVisibility(View.GONE);
        }

        if (StringUtils.isEmpty(baoJingFuHeModel.getStatus())){
            tv_status.setVisibility(View.GONE);
        }else {
            tv_status.setVisibility(View.VISIBLE);
            if ("0".equals(baoJingFuHeModel.getStatus())){
                tv_status.setText("待复核");
                tv_status.setTextColor(Color.parseColor("#FD9D3F"));
            }else {
                if (StringUtils.isEmpty(baoJingFuHeModel.getIsAlarm()) || "0".equals(baoJingFuHeModel.getIsAlarm())) {
                    tv_status.setText("已复核");
                    tv_status.setTextColor(Color.parseColor("#5573FF"));

                }else if("1".equals(baoJingFuHeModel.getIsAlarm())) {
                    GradientDrawable gradientDrawable = new GradientDrawable();
                    gradientDrawable.setCornerRadius(Utils.dip2px(activity, 2));
                    gradientDrawable.setColor(activity.getResources().getColor(R.color.main_color));
                    tv_status.setBackground(gradientDrawable);
                    tv_status.setText("真警");
                    tv_status.setTextColor(activity.getResources().getColor(R.color.white));
                    bt_zhenjing.setVisibility(View.GONE);
                    bt_wubao.setVisibility(View.GONE);
                }else {
                    GradientDrawable gradientDrawable = new GradientDrawable();
                    gradientDrawable.setCornerRadius(Utils.dip2px(activity, 2));
                    gradientDrawable.setColor(activity.getResources().getColor(R.color.change_status_color));
                    tv_status.setBackground(gradientDrawable);
                    tv_status.setText("误报");
                    tv_status.setTextColor(activity.getResources().getColor(R.color.white));
                    bt_zhenjing.setVisibility(View.GONE);
                    bt_wubao.setVisibility(View.GONE);
                }
            }
        }
        tv_time.setText(baoJingFuHeModel.getWarningTimeStr());
        StringBuilder stringBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(baoJingFuHeModel.getBuildingName())){
            stringBuilder.append(baoJingFuHeModel.getBuildingName());
        }
        if (!StringUtils.isEmpty(baoJingFuHeModel.getFloorName())){
            stringBuilder.append(baoJingFuHeModel.getFloorName());
        }
        if (!StringUtils.isEmpty(baoJingFuHeModel.getAddress())){
            stringBuilder.append(baoJingFuHeModel.getAddress());
        }
        tv_location.setText(stringBuilder);

        // 描述信息
        if (StringUtils.isEmpty(baoJingFuHeModel.getRemark())){
            ll_intro.setVisibility(View.VISIBLE);
        }else {
            ll_intro.setVisibility(View.VISIBLE);
            tv_intro.setText(baoJingFuHeModel.getRemark());
        }
        // 现场图片
        if (StringUtils.isEmpty(baoJingFuHeModel.getImgUrls())){
            include_play_photo_three.setVisibility(View.GONE);
        }else {
            include_play_photo_three.setVisibility(View.VISIBLE);
            String[] imageUrls = baoJingFuHeModel.getImgUrls().split(",");
            if (imageUrls.length >= 1 && imageUrls[0].startsWith("http")){
                iv_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imageUrls[0]);
                iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((ShowFragActivity)activity).setShowImg(imageUrls[0]);
                    }
                });
            }else {
                iv_device_photo_1.setVisibility(View.INVISIBLE);
            }
            if (imageUrls.length >= 2){
                iv_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imageUrls[1]);
                iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((ShowFragActivity)activity).setShowImg(imageUrls[1]);
                    }
                });
            }else {
                iv_device_photo_2.setVisibility(View.INVISIBLE);
            }
            if (imageUrls.length >= 3){
                iv_device_photo_3.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_3, imageUrls[2]);
                iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((ShowFragActivity)activity).setShowImg(imageUrls[2]);
                    }
                });
            }else {
                iv_device_photo_3.setVisibility(View.INVISIBLE);
            }
        }

        switch (baoJingFuHeModel.getType()){

            // 监管

            case ConstData.JIANGUAN_STATUS://
                ll_fuhe_deal.setVisibility(View.VISIBLE);
            //    bt_submit.setVisibility(View.GONE);

    /*            if ("0".equals(baoJingFuHeModel.getStatus())) {// 待复核
                    bt_submit.setVisibility(View.VISIBLE);
                }else {// 已复核
                    bt_submit.setVisibility(View.GONE);
                }*/
                break;
            case ConstData.HUOJING_STATUS://
            case ConstData.YUJING_STATUS://
                bt_submit.setVisibility(View.GONE);
                if ("0".equals(baoJingFuHeModel.getStatus())) {
                    // 待复核
                    ll_fuhe_deal.setVisibility(View.VISIBLE);
                }else {// 已复核
                    ll_fuhe_deal.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DialogUtils.closeDialog(zhenjingDialog);
    }

    @Override
    public void onResume() {
        super.onResume();
        if ("0".equals(PreferencesUtils.getString(activity, ConstData.WUBAOREFRESH, "1"))){
            PreferencesUtils.putString(activity, ConstData.WUBAOREFRESH, "1");
            activity.finish();
        }
    }
}
